package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.pojo.AjaxResult;
import com.education.common.pojo.CatalogResultVO;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.pojo.Review;
import com.education.welco.service.ISCatalogMenuService;
import com.education.welco.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 审核表
 *
 * @date 2020-12-04 13:52:48
 */
@RestController
@RequestMapping("/welco/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private ISCatalogMenuService isCatalogMenuService;

    //查询全部数据
    @GetMapping("/queryList")
    public AjaxResult queryList(Review review, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<Review> list = reviewService.selectReviewList(review);
        for (Review revi : list) {
            Map<String, Object> namestr = isCatalogMenuService.selectNameById(revi.getCatalogId());
            if (StringUtils.isEmpty(namestr)) continue;
            String s = (String) namestr.get("onName") + "/" + namestr.get("twNmae") + "/" + namestr.get("thName");
            revi.setCatalogName(s);
        }
        return AjaxResult.success(new PageInfo<Review>(list));
    }

    //添加数据
    @PostMapping("/insert")
    public ApiReturnObject insert(@RequestBody CatalogResultVO resultVO){
        for (Integer id : resultVO.getIds()) {
            Review review = new Review();
            review.setUserId(resultVO.getUserId());
            review.setCatalogId(id);
            review.setParentId(resultVO.getParentId());
            review.setPrice(resultVO.getPrice());
            review.setCreateTime(new Date());
            review.setIsRed("2");
            int i = reviewService.insertReview(review);
            if (i > 0) return new ApiReturnObject("200","添加成功","");
        }
        return new ApiReturnObject("201","添加失败","");
    }

    //修改数据
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody Review review){
        int i = reviewService.updateReview(review);
        if (i > 0) return new ApiReturnObject("200","修改成功","");
        return new ApiReturnObject("201","修改失败","");
    }

    //批量删除
    @GetMapping("/delete")
    public ApiReturnObject delete(String[] ids) {
        int i = reviewService.deleteReviewByIds(ids);
        if (i > 0) return new ApiReturnObject("200","删除成功","");
        return new ApiReturnObject("201","删除失败","");
    }
}
